package net.xo68.destiny.manager.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import net.xo68.destiny.contract.domain.user.UserRole;
import net.xo68.destiny.contract.enums.user.UserRoleEnum;
import net.xo68.destiny.manager.security.SecurityUser;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户登录返回结果
 * @author wuxie
 * @version 2020-11-8
 */
@ApiModel("用户登录结果")
@Data
public class UserLoginResultDto implements Serializable {

    @ApiModelProperty("用户令牌")
    private String token;
    @ApiModelProperty("用户编号")
    private Long userId;
    @ApiModelProperty("昵称")
    private String userNice;
    @ApiModelProperty("角色")
    private Set<UserRole> roles;
    @ApiModelProperty("过期时间毫秒数")
    private long expired;

    public static UserLoginResultDto of(SecurityUser securityUser,String token,long expireSecond){
        UserLoginResultDto userLoginResult=new UserLoginResultDto();
        userLoginResult.setExpired(System.currentTimeMillis() + expireSecond * 1000);
        userLoginResult.setUserId(securityUser.getId());
        userLoginResult.setUserNice(securityUser.getNice());

        final Set<UserRole> roleSet= new HashSet<>();
        securityUser.getRoleSet().forEach(role->{
            UserRoleEnum roleEnum=UserRoleEnum.valueOf(role);
            roleSet.add(roleEnum.getUserRole());
        });
        userLoginResult.setRoles(roleSet);
        userLoginResult.setToken(token);
        return userLoginResult;
    }
}
